package com.controller;

import com.dao.AdministratorDao;
import com.dao.InboxDao;
import com.po.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet({"/selectFrozen", "/unFreezing", "/setFreezing", "/requestUnFreezing", "/applyFreezing"})
public class AdministratorSystem extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String servletPath = request.getServletPath();
        if ("/selectFrozen".equals(servletPath)) {
            doSelectFrozen(request, response);
        } else if ("/unFreezing".equals(servletPath)) {
            doUnFreezing(request, response);
        } else if ("/setFreezing".equals(servletPath)) {
            doSetFreezing(request, response);
        } else if ("/requestUnFreezing".equals(servletPath)) {
            doRequest(request, response);
        } else if ("/applyFreezing".equals(servletPath)) {
            doApply(request, response);
        }

    }

    /**
     * 接受冻结用户的申请
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void doApply(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String state = request.getParameter("state");
        int target_id = Integer.parseInt(request.getParameter("target_id"));

        if ("agree".equals(state)) {
            AdministratorDao.relieveFrozen(target_id);
        }
        InboxDao.deleteAdd(target_id, 1, "freezing");
        response.sendRedirect(request.getContextPath() + "/friend/myInbox");
    }

    /**
     * 冻结用户申请冻结内容
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void doRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String request_content = request.getParameter("request_content");
        int id = Integer.parseInt(request.getParameter("id"));

        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();

        if (request_content.length() >= 1) {
            if (InboxDao.ifExit(id, 1, "freezing")) {
                InboxDao.inboxTip(id, 1, "freezing", request_content);
                out.println("已经发送申请，请等待");
                out.println("<a href=\"" + request.getContextPath() + "/index.jsp\">返回登录</a>");
            }else{
                out.print("您已经发送过申请了，请耐心等待管理员的回应");
                out.println("<a href=\"" + request.getContextPath() + "/index.jsp\">返回登录页面</a>");
            }
        } else {
            out.print("输入的申请不能为空");
        }
    }

    /**
     * 系统管理员冻结用户
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void doSetFreezing(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");

        int envelope_id = Integer.parseInt(request.getParameter("envelope_id"));
        String envelope_name = request.getParameter("envelope_name");
        int note_id = Integer.parseInt(request.getParameter("note_id"));

        AdministratorDao.setFreezing(note_id);
        response.sendRedirect(request.getContextPath() + "/function/selectNotes?envelope_id=" + envelope_id + "&envelope_name=" + envelope_name);
    }

    /**
     * 系统管理员解冻某个用户
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void doUnFreezing(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("UTF-8");
        int user_id = Integer.parseInt(request.getParameter("user_id"));
        AdministratorDao.relieveFrozen(user_id);
        response.sendRedirect(request.getContextPath() + "/selectFrozen");

    }

    /**
     * 查看所有被冻结的用户
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void doSelectFrozen(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        List<User> frozenUsers = AdministratorDao.getFrozenUser();
        request.setAttribute("frozenUsers", frozenUsers);
        request.getRequestDispatcher("/administrator/frozenUsers.jsp").forward(request, response);
    }
}
